/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.cpnc.mm.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author Luis
 */
public class DefaultUnitTreeCellRenderer extends DefaultTreeCellRenderer {
    
    public DefaultUnitTreeCellRenderer(){
        this.setFont(new Font(Font.DIALOG, Font.PLAIN, 16));
    }

    @Override
    public Color getBackground() {
        return null;
    }

    @Override
    public Color getBackgroundNonSelectionColor() {
        return null;
    }

    @Override
    public Color getBackgroundSelectionColor() {
        return new Color(245,240,240);
    }
    
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus){
        Object o = ((DefaultMutableTreeNode)value).getUserObject();
        if(o instanceof pe.edu.pucp.cpnc.mm.bean.Object){
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            pe.edu.pucp.cpnc.mm.bean.Object o2 =  (pe.edu.pucp.cpnc.mm.bean.Object)o;
            BufferedImage scaledImg = new BufferedImage(40, 40, BufferedImage.TYPE_INT_ARGB);
            
            //Scaling
            Graphics2D g = scaledImg.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawImage(MainFrame.rd.getSprite(o2.getSpriteToRender(0)), 0, 0, 
                    scaledImg.getWidth(), scaledImg.getHeight(), null);
            g.dispose();
            
            this.setIcon(new ImageIcon(scaledImg));
            return this;
        }else{
            return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        }
    }
}
